package com.gprcclient.grpcapi;

import com.gprcclient.convent.GoodInfoConvent;
import com.gprcclient.entity.GoodInfo;
import com.grpcclient.api.CreateUserRequest;
import com.grpcclient.api.UserGrpc;
import com.grpcclient.api.UserInfo;
import com.grpcclient.api.UserReply;
import com.grpcclient.goods.*;
import io.grpc.Channel;
import io.grpc.ManagedChannelBuilder;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author: LCG
 * @date: 2022-10-25 17:13:02
 * @description:
 **/
@Service
public class TestGrpcService {

    @GrpcClient("grpc-server1")
    private UserGrpc.UserFutureStub userGrpc;

    @GrpcClient("grpc-server1")
    private GoodsGrpc.GoodsFutureStub goodsFutureStub;

    public Object test01() throws ExecutionException, InterruptedException {
        CreateUserRequest.Builder builder = CreateUserRequest.newBuilder();
        builder.setNickname("renboguo");
        Channel channel = userGrpc.getChannel();

        UserReply userReply = userGrpc.createUser(builder.build()).get();

        UserInfo data = userReply.getData();

        return data.getId()+"--"+data.getMobile()+"---"+data.getNickname()+"--token--"+data.getToken();
    }

    public Object test02() throws ExecutionException, InterruptedException {
        GoodsId.Builder builder = GoodsId.newBuilder().setGoodsId("123456");
        GoodsInfoList goodsInfoList = goodsFutureStub.getGoodsList(builder.build()).get();
        List<GoodsInfo> goodsInfoList1 = goodsInfoList.getGoodsInfoList();
        List<GoodInfo> goodInfos = GoodInfoConvent.INSTANCE.toGoodInfoList(goodsInfoList1);
        return goodInfos;
    }

}
